<!--
  移动签批->公文详情->发文->填写意见
-->
<template>
  <view>
    <van-popup v-model="showCompo" closeable round position="bottom" :style="{ height: '80%' }"
               @click-close-icon="toClose" @click-overlay="toClose">
      <van-row type="flex" justify="center">
        <van-col offset="3" span="8">
          <van-cell value="填写意见"/>
        </van-col>
      </van-row>
      <van-form @submit="onSubmit">
        <van-cell is-link title="意见类型" v-model="yjlx" @click="showyjlx = true"/>
        <van-action-sheet ref="yjlx" v-model="showyjlx" :actions="yjlxList" cancel-text="取消"
                          close-on-click-action @select="yjlxChange"/>


        <van-cell is-link title="批示方式" v-model="psfs" @click="showpsfs = true"/>
        <van-action-sheet ref="psfs" v-model="showpsfs" :actions="psfsList" cancel-text="取消"
                          close-on-click-action @select="psfsChange"/>

        <template v-if="showOpinion">
          <van-cell title="填写意见" :border="border"></van-cell>
          <van-field
                  v-model="message"
                  rows="2"
                  autosize
                  type="textarea"
                  placeholder="请输入"
                  clearable
          />
        </template>
        <!-- <van-field v-model="message" rows="2" :autosize="autosize" type="textarea" placeholder="请输入" clearable  :rules="[{ required: true, message: '请填写用户名' }]"/> -->
        <!-- <van-cell title="已上传文件" value="民航旅客中转便利化实施指南.docx" value-class="text-sm" /> -->


        <view class="css">
          <van-row type="flex" justify="space-around">
            <van-col span="10">
              <van-button type="info" round plain hairline block @click.prevent="toClose">取消</van-button>
            </van-col>
            <van-col span="10">
              <van-button type="info" round block native-type="onSubmit">确定</van-button>
            </van-col>
          </van-row>
          <!--
                    <van-row v-if="!showTmsBtn" type="flex" justify="space-around">
                      <van-col span="10">
                        <van-button type="info" round plain hairline block @click.prevent="toClose">取消</van-button>
                      </van-col>
                      <van-col span="10">
                        <van-button type="info" round block  @click.prevent="onSubmit">确定</van-button>
                      </van-col>
                    </van-row>
                    <van-row v-else type="flex" justify="space-around">
                      <van-col span="6">
                        <van-button type="info" round plain hairline block @click.prevent="toClose">取消</van-button>
                      </van-col>
                      <van-col span="8">
                        <van-button type="info" round block  @click.prevent="onSubmitReturnMs">确认并退秘书</van-button>
                      </van-col>
                      <van-col span="6">
                        <van-button type="info" round block  @click.prevent="onSubmit">确定</van-button>
                      </van-col>
                    </van-row>
                    -->
        </view>

      </van-form>
    </van-popup>
  </view>
</template>

<script>
export default {
  components: {},
  props: {
    showRecords: {
      type: Boolean,
      default: false
    },
    paramObject: {
      type: Object
    }
  },
  data() {
    return {
      showCompo: false,//组件是否显示
      autosize: {
        maxHeight: 50,
        minHeight: 30
      },
      showOpinion: false,//显示填写意见框
      border: false,
      yjlx: '请选择意见类型',
      psfs: '请选择批示方式',
      showyjlx: false,
      showpsfs: false,
      //意见类型
      yjlxList: [],
      //批示方式
      psfsList: [],
      //填写意见
      message: '',
      reviewType: '',//意见类型
      opiniontype: '',
      resultData: [],//字典获取所有意见类型
      jldReviewVal: '',
      showTmsBtn: false,//填写意见时是否显示退秘书button
      toActDefId: '',
    };
  },
  mounted() {
    //默认值
    this.yjlx = ''
    this.psfs = '签署意见'
    this.opiniontype = 'nibanReview'
    this.resultData = this.cache.get('reviewType')//意见类型的字典值数组
  },
  watch: {
    //{"bizId":"26c01daaf42448b68e0a540a42fb9cf3","proInstId":"01aead9032f240e6812c259af6cc9323","configId":"2d3327240f0f41c9843ac1c809e64a95","proDirId":"d41474fb261647528cf4d7f16d47af4d","actInstId":"0dc9bf1336394634a72d9f5acb1825b4","actDefId":"sjzbsgzz","workitemId":"45232f9187964507b49c660bb2b5c052","state":null,"isClosed":"2","configCode":"glj_fw"}
    paramObject: {
      async handler(newValue, oldValue) {
        //如果子组件传值过来了，请求该方法
        if (this.util.isNotEmpty(newValue)) {
          await this.getOpinionData(newValue.proInstId)
          await this.getCaacOpinion(newValue.proInstId)
          //填写意见时是否显示退秘书button
          if (this.util.isNotEmpty(newValue.actDefId)) {
            if (newValue.actDefId == 'bmldsh' || newValue.actDefId == 'bmld') {

              this.$http({
                url: this.$http.adornUrl(`/caac/wfm/ApplicationController/listNextActivities`),
                method: 'post',
                params: {
                  'systemKey': this.cache.get('userInfo').systemKey,//固定值
                  'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
                  'data': this.$http.adornData({
                    wfmData: {
                      'configId': newValue.configId,
                      'proDirId': newValue.proDirId,//发文详情返回的proInstId
                      'actDefId': newValue.actDefId,//
                      'actInstId': newValue.actInstId,//
                      'workitemId': newValue.workitemId,
                      'wfmInitPageType': 'running'
                    }
                  })
                }
              }).then(({data}) => {
                if (this.util.isNotEmpty(data.data.nextActDefIds)) {
                  let nextActDefIds = data.data.nextActDefIds
                  for (let i = 0; i < nextActDefIds.length; i++) {
                    let nextActivity = nextActDefIds[i];
                    if (nextActivity.actDefId === 'mszz' || nextActivity.actDefId === 'sms') {
                      this.toActDefId = nextActivity.actDefId;
                    }
                  }
                  if (this.util.isNotEmpty(this.toActDefId)) {
                    this.showTmsBtn = true
                  }
                }
              })
            }
          }
        }
      },
      immediate: true,
      deep: true
    },
    showRecords(val) {
      this.showCompo = val
    },
    psfs: {
      handler(nVal, oVal) {
        console.log("nVal====>" + nVal)
        //如果是紧签名，则隐藏意见填写框
        if (nVal == '仅签名') {
          this.showOpinion = false
        } else {
          this.showOpinion = true
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    //意见类型
    yjlxChange(item) {
      //item {"name":"拟办意见","value":"nibanReview"}
      this.showyjlx = false;
      this.yjlx = item.name;
      this.opiniontype = item.value
      this.psfsList = []
      if (item.value == 'nibanReview') {
        this.psfsList.push({'value': '2', 'name': "签署意见"})
        this.psfs = '签署意见'
        // this.message = this.cache.get('userInfo').userName
      } else {
        this.psfsList.push({'value': '1', 'name': "仅签名"}, {'value': '2', 'name': "签署意见"})
      }
    },

    //批示方式
    psfsChange(item) {
      //{"value":"2","name":"签署意见"}
      this.showpsfs = false;
      this.psfs = item.name;
      if (item.value == '1') {//仅签名
        this.message = this.cache.get('userInfo').userName
      } else {
        this.message = ''
        this.getCaacOpinion(this.paramObject.proInstId)
      }
    },
    //确认并退司秘书
    onSubmitReturnMs() {
      if (this.util.isEmpty(this.message)) {//如果显示意见框,则显示退秘书按钮
        this.$toast("请先填写意见")
      } else {
        this.$http({
          url: this.$http.adornUrl(`/caac/oa/component/approval/saveopinion`),
          method: 'post',
          params: {
            'systemKey': this.cache.get('userInfo').systemKey,//固定值
            'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
          },
          data: this.$http.adornData({
            'proInstId': this.paramObject.proInstId,//发文详情返回的proInstId
            'workItemId': this.paramObject.workitemId,//
            'actInstId': this.paramObject.actInstId,//
            'opiniontype': this.opiniontype,
            'opinion': this.message,
            'isApproval': '1',//核稿状态，默认传1
          })
        }).then(({data}) => {
          if (data.code == 200) {
            this.$emit('saveOpinionAndSendWithoutSelectUser', this.toActDefId)
          } else {
            this.$toast(data.message + ' code:' + data.code)
          }

        })

      }
    },
    onSubmit() {
      this.saveopinion();
      this.$emit('closeRecords', this.showRecords);
    },
    toClose() {
      this.$emit('closeRecords', this.showRecords);
    },
    //获取意见类型
    async getOpinionData(proInstId) {
      await this.$http({
        url: this.$http.adornUrl(`/caac/oa/app/getOpinionData`),
        method: 'get',
        params: this.$http.adornParams({
          'proInstId': proInstId,
          'canGotoSpecialDraft': 'true',
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
        })
      }).then(({data}) => {
        //{"code":200,"data":{"opinion":[{"dictItemName":"拟办意见","dictItemCode":"nibanReview"},{"dictItemName":"部门负责人审签","dictItemCode":"sldReview"},{"dictItemName":"签发意见","dictItemCode":"jldReview"},{"dictItemName":"主办部门负责人审签","dictItemCode":"cldReview"}]},"message":null}
        if (data.code == 200) {
          if (this.util.isNotEmpty(data.data.opinion)) {
            this.yjlxList = []//清空数据
            data.data.opinion.forEach((item) => {
              this.yjlxList.push({"name": item.dictItemName, "value": item.dictItemCode})
            })
            //选项默认值
            if (this.util.isNotEmpty(this.yjlxList)) {
              this.yjlx = this.yjlxList[0].name;
              this.opiniontype = this.yjlxList[0].value
              this.psfsList = []
              if (this.opiniontype == 'nibanReview') {
                this.psfsList.push({'value': '2', 'name': "签署意见"})
                this.psfs = '签署意见'
                // this.message = this.cache.get('userInfo').userName
              } else {
                this.psfsList.push({'value': '1', 'name': "仅签名"}, {'value': '2', 'name': "签署意见"})
              }
            }
          }
        }
      })
    },
    //获取意见,用来回显意见
    async getCaacOpinion(proInstId) {
      let workItemId = this.paramObject.workitemId;
      await this.$http({
        url: this.$http.adornUrl(`/caac/oa/component/approval/approvalwrite`),
        method: 'get',
        params: this.$http.adornParams({
          'proInstId': proInstId,
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
        })
      }).then(({data}) => {
        if (data.code == 200) {
          Object.keys(data.data).forEach((item, index) => {
            let subItem = data.data[item]
            if (subItem.length > 0) {
              for (let i = 0; i < subItem.length; i++) {
                //如果之前填写意见了那就在这个
                let approvalWorkItemId = subItem[i].workItemId;
                let approver = subItem[i].approver;
                let userId = this.cache.get('userInfo').userId
                if (approvalWorkItemId == workItemId && approver == userId) {

                  this.message = subItem[i].opinion;//意见回显
                  this.resultData.forEach((value) => {
                    if (value.dictItemCode == item) {
                      this.yjlx = value.dictItemName
                      this.opiniontype = value.dictItemCode
                    }
                  })
                }
              }
            }
          })
        }
      })
    },
    //填写意见接口
    saveopinion() {
      if (this.util.isEmpty(this.message)) {
        this.message = this.cache.get('userInfo').userName
      }
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/component/approval/saveopinion`),
        method: 'post',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
        }),
        data: this.$http.adornData({
          'proInstId': this.paramObject.proInstId,//发文详情返回的proInstId
          'workItemId': this.paramObject.workitemId,//
          'actInstId': this.paramObject.actInstId,//
          'opiniontype': this.opiniontype,
          'opinion': this.message,
          'isApproval': '1',//核稿状态，默认传1
        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.$emit('refreshBtnGroup', true)//通知父组件重新加载数据
          this.$toast('意见填写成功')
        } else {
          this.$toast(data.message + ' code:' + data.code)
        }
      })
    },

  },
};
</script>

<style scoped>
.css {
  text-align: center;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 3%;
}

.van-cell__title {
  min-width: 50%;

span {
  display: inline-block;
  text-align: left;
  word-break: break-all;
}
}

/deep/ .van-field__control {
  font-size: 14px;
  color: #969799;
}
</style>
